<?php
if( @$_POST['cities'] && $_POST['towns'] ) {
	$TBL_CITY 	= 'city';
	$TBL_TOWN   = 'town';
	$sqls[] = 'CREATE TABLE `'.$TBL_CITY.'` (
	  `code` varchar(255) DEFAULT NULL,
	  `name` varchar(255) DEFAULT NULL
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
	$sqls[] = 'CREATE TABLE `'.$TBL_TOWN.'` (
	  `code` varchar(255) DEFAULT NULL,
	  `town` varchar(255) DEFAULT NULL,
	  `name` varchar(255) DEFAULT NULL
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
	$cities 	= json_decode($_POST['cities']);
	$towns		= json_decode($_POST['towns']);
	foreach( $cities as $city ){
		$city_ID = $city[0];
		$city_NAME = $city[1];
		$sqls[] = "INSERT INTO $TBL_CITY VALUES('$city_ID','$city_NAME');";
	}
	foreach( $towns as $town ) {
		$city_ID = $town[0];
		$town_ID = $town[1];
		$town_NAME = $town[2];
		$sqls[] = "INSERT INTO $TBL_TOWN VALUES('$city_ID','$town_ID','$town_NAME');";
	}
	file_put_contents('pk.sql',implode(PHP_EOL,$sqls));
}
?>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9">
	<script type="text/javascript" charset="iso-8859-9" src="https://interaktifkargo.ptt.gov.tr/posta_kodu/Posta_20130204.js"></script>
	<script type="text/javascript" charset="utf-8" src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script>
	<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
	<script type="text/javascript">
		$(function() {
			var response = $.post('pk.php',{ cities : JSON.stringify(x) , towns : JSON.stringify(y) } ,function(data) { $("#response").html(data); });
		});
	</script>
</head>
<body>
	<a href="pk.sql">Download PK <?php echo date('d m Y'); ?></a>
</body>
</html>